package ewms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ewms.bo.EwmsOutFactoryAddBo;
import ewms.bo.EwmsOutFactoryEditBo;
import ewms.bo.EwmsOutFactoryQueryBo;
import ewms.domain.EwmsOutFactory;
import ewms.mapper.EwmsOutFactoryMapper;
import ewms.service.IEwmsOutFactoryService;
import ewms.vo.EwmsOutFactoryVo;
import ewms.common.core.page.PagePlus;
import ewms.common.core.page.TableDataInfo;
import ewms.common.exception.CustomException;
import ewms.common.utils.PageUtils;
import ewms.domain.EwmsWasteDetail;
import ewms.mapper.EwmsWasteDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 委外出厂Service业务层处理
 *
 * @author zhaoxiaochun
 * @date 2022-02-17
 */
@Service
public class EwmsOutFactoryServiceImpl extends ServiceImpl<EwmsOutFactoryMapper, EwmsOutFactory> implements IEwmsOutFactoryService {

	@Autowired
	private EwmsWasteDetailMapper wasteDetailMapper;

	@Override
	public EwmsOutFactoryVo queryById(Long outFactoryId) {
		return getVoById(outFactoryId, EwmsOutFactoryVo.class);
	}

	@Override
	public TableDataInfo<EwmsOutFactoryVo> queryPageList(EwmsOutFactoryQueryBo bo) {
		PagePlus<EwmsOutFactory, EwmsOutFactoryVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), EwmsOutFactoryVo.class);
		List<EwmsOutFactoryVo> recordList = result.getRecordsVo();
		handleOverDay(recordList);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<EwmsOutFactoryVo> queryList(EwmsOutFactoryQueryBo bo) {
		return handleOverDay(listVo(buildQueryWrapper(bo), EwmsOutFactoryVo.class));
	}

	private LambdaQueryWrapper<EwmsOutFactory> buildQueryWrapper(EwmsOutFactoryQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<EwmsOutFactory> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getWasteId() != null, EwmsOutFactory::getWasteId, bo.getWasteId());
		lqw.like(StrUtil.isNotBlank(bo.getWasteName()), EwmsOutFactory::getWasteName, bo.getWasteName());
		lqw.like(StrUtil.isNotBlank(bo.getTransferNumber()), EwmsOutFactory::getTransferNumber, bo.getTransferNumber());
		lqw.eq(StrUtil.isNotBlank(bo.getHandleCompanyKey()), EwmsOutFactory::getHandleCompanyKey, bo.getHandleCompanyKey());
		lqw.between(params.get("beginOutFactoryDate") != null && params.get("endOutFactoryDate") != null,
			EwmsOutFactory::getOutFactoryDate, params.get("beginOutFactoryDate"), params.get("endOutFactoryDate"));
		lqw.orderByDesc(EwmsOutFactory::getOutFactoryDate);
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(EwmsOutFactoryAddBo bo) {
		EwmsWasteDetail ewmsWasteDetail = wasteDetailMapper.selectById(bo.getWasteId());
		if (Objects.isNull(wasteDetailMapper)) {
			throw new CustomException("危废信息未找到");
		}
		EwmsOutFactory add = BeanUtil.toBean(bo, EwmsOutFactory.class);
		add.setWasteName(ewmsWasteDetail.getWasteName());
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(EwmsOutFactoryEditBo bo) {
		EwmsOutFactory update = BeanUtil.toBean(bo, EwmsOutFactory.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(EwmsOutFactory entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}

	/**
	 * 处理超过天数
	 *
	 * @param list
	 */
	private static List<EwmsOutFactoryVo> handleOverDay(List<EwmsOutFactoryVo> list) {
		list.forEach(vo -> {
			Date outFactoryDate = vo.getOutFactoryDate();
			vo.setOverDay(DateUtil.betweenDay(outFactoryDate, DateUtil.date(), true));
		});

		return list;
	}
}
